package com.sc.nft.mapper;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dtflys.forest.annotation.Query;
import com.sc.nft.dto.EcologyGetDestructionDTO;
import com.sc.nft.dto.SumEcologyPointsGroupUserDTO;
import com.sc.nft.dto.UserEcologyDTO;
import com.sc.nft.dto.UserHatchCountDTO;
import com.sc.nft.entity.UserEcologyHatchRecords;
import com.sc.nft.entity.vo.EcologySmeltVO;
import com.sc.nft.entity.vo.PageHatchRecordsVO;
import com.sc.nft.entity.vo.ecology.*;
import com.sc.nft.enums.UserEcologyHatchRecordsStatus;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 生态-孵化-用户记录表(sc_user_ecology_hatch_records)数据Mapper
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-10-13 17:22:12
 */
@Mapper
public interface UserEcologyHatchRecordsMapper extends BaseMapper<UserEcologyHatchRecords> {

    @Select("SELECT IFNULL(SUM(real_ecology_points),0) as sum_ecology_points FROM sc_user_ecology_hatch_records WHERE user_id = #{userId} AND is_delete = 0")
    BigDecimal getSumEcologyPointsByUserId(@Param("userId") Long userId);

    /**
     * 分页获取孵化记录
     *
     * @param page
     * @param userId
     * @return
     */
    Page<PageHatchRecordsVO> pageHatchRecords(Page page, @Param("userId") Long userId);

    /**
     * 获取下级熔炼记录
     *
     * @param ids
     * @return
     */
    Page<JunioRecologyHatchRecords> getJunioRecologyHatchRecordsByUserId(Page page, @Param("ids") List<Long> ids);

    /**
     * 修改熔炼记录为已结束
     * 条件: 1. already_day>= hatch_cycle
     * 2. status = 1
     *
     * @return
     */
    boolean updateStatusByAlreadyDay();

    /**
     * 获取指定用户熔炼记录是在进行中中的并且大于指定数量的记录的用户id
     *
     * @param userIds
     * @param status
     * @param count
     * @return
     */
    List<Long> getListByUserIdsAndStatusAndGtCount(@Param("userIds") List<Long> userIds, @Param("status") Integer status, @Param("count") int count);

    /**
     * 获取指定用户指定熔炼记录是在进行中中的并且大于指定数量的记录的用户id
     *
     * @param userIds
     * @param status
     * @param count
     * @return
     */
    List<Long> getListByUserIdsAndStatusAndGtCountAndCollectionId(@Param("userIds") List<Long> userIds, @Param("status") Integer status, @Param("count") int count,@Param("hatchCollectionId")Long hatchCollectionId);


    /**
     * 获取分红池
     *
     * @param day
     * @return
     */
    BigDecimal getBonusPool(@Param("day") int day);

    /**
     * 获取指定用户的熔炼积分
     *
     * @param userIds
     * @return
     */
    BigDecimal getSumPriceByUserIdListAndDay(@Param("userIds") List<Long> userIds, @Param("day") Integer day);

    /**
     * 获取指定用户的熔炼组数
     * @param userIds
     * @param day
     * @return
     */
    BigDecimal getHatchCountByUserIdListAndDayAndCollectionId(@Param("userIds") List<Long> userIds, @Param("day") Integer day,@Param("hatchCollectionId")Long hatchCollectionId);

    /**
     * 获取指定用户的熔炼指定藏品熔炼积分
     *
     * @param userIds
     * @param collectionIds
     * @param day
     * @return
     */
    BigDecimal getSumPriceByUserIdListAndDayAndCollectionIds(@Param("userIds") List<Long> userIds, @Param("collectionIds") List<Long> collectionIds, @Param("day") Integer day);

    /**
     * 获取指定用户的熔炼指定藏品熔炼积分(上周)
     */
    BigDecimal getSumPriceByUserIdListAndLastWeekAndCollectionIds(@Param("userIds") List<Long> userIds, @Param("collectionIds") List<Long> collectionIds, @Param("beginOfWeek") DateTime beginOfWeek, @Param("endOfWeek") DateTime endOfWeek);

    /**
     * 获取上周有效业绩
     *
     * @param userIds
     * @param beginOfWeek
     * @param endOfWeek
     * @return
     */
    BigDecimal getSumPriceByUserIdListAndLastWeek(@Param("userIds") List<Long> userIds, @Param("beginOfWeek") Date beginOfWeek, @Param("endOfWeek") Date endOfWeek);

    /**
     * 上周熔炼组数
     * @param userIds
     * @param beginOfWeek
     * @param endOfWeek
     * @param hatchCollectionId
     * @return
     */
    BigDecimal getSumPriceByUserIdListAndLastWeekAndCollectionId(@Param("userIds") List<Long> userIds, @Param("beginOfWeek") Date beginOfWeek, @Param("endOfWeek") Date endOfWeek,@Param("hatchCollectionId")Long hatchCollectionId);

    /**
     * 获取近天数内 拥有铸造记录的人数
     *
     * @param userIds
     * @param status
     * @param day
     * @return
     */
    List<UserEcologyHatchRecords> getCountByUserIdsAndStatusAndDay(@Param("userIds") List<Long> userIds, @Param("status") int status, @Param("day") Integer day);

    /**
     * 获取上周拥有铸造记录的人数
     *
     * @param userIds
     * @param status
     * @param beginOfWeek
     * @param endOfWeek
     * @return
     */
    List<UserEcologyHatchRecords> getCountByUserIdsAndStatusAndLastWeek(@Param("userIds") List<Long> userIds, @Param("status") int status, @Param("beginOfWeek") Date beginOfWeek, @Param("endOfWeek") DateTime endOfWeek);

    /**
     * 查询指定用户id 指定藏品id 指定状态的记录
     *
     * @param userIds
     * @param status
     * @param day
     * @return
     */
    List<UserEcologyHatchRecords> getCountByUserIdsAndEcologyHatchcollectionIdsAndStatusAndDay(@Param("userIds") List<Long> userIds, @Param("collectionIds") List<Long> collectionIds, @Param("status") int status, @Param("day") Integer day);


    List<UserEcologyHatchRecords> getCountByUserIdsAndEcologyHatchCollectionIdsAndStatusAndDayPoints(@Param("userIds") List<Long> userIds, @Param("collectionId") Long collectionId, @Param("status") int status, @Param("day") Integer day);

    /**
     * 查询指定用户id 指定藏品id 指定状态的记录(上周)
     *
     * @param userIds
     * @param collectionIds
     * @param status
     * @param beginOfWeek
     * @param endOfWeek
     * @return
     */
    List<UserEcologyHatchRecords> getCountByUserIdsAndEcologyHatchcollectionIdsAndStatusAndLastWeek(@Param("userIds") List<Long> userIds, @Param("collectionIds") List<Long> collectionIds, @Param("status") int status, @Param("beginOfWeek") Date beginOfWeek, @Param("endOfWeek") DateTime endOfWeek);

    List<UserEcologyHatchRecords> getCountByUserIdsAndEcologyHatchCollectionIdsAndStatusAndLastWeekPoints(@Param("userIds") List<Long> userIds, @Param("collectionId") Long collectionId, @Param("status") int status, @Param("beginOfWeek") Date beginOfWeek, @Param("endOfWeek") DateTime endOfWeek);

    List<Long> recentlyDaySmelt(@Param("userIds") List<Long> userIds, @Param("code") int code, @Param("day") int day,@Param("collectionId") Long collectionId);

    List<Long> recentlyDaySmeltLastWeek(@Param("userIds") List<Long> userIds, @Param("code") int code,
                                        @Param("beginOfWeek") Date beginOfWeek, @Param("endOfWeek") Date endOfWeek,
                                        @Param("collectionId") Long collectionId);

    /**
     * 获取指定状态的所有用户id  已用户分组
     *
     * @param userIds
     * @param status
     * @param day
     * @return
     */
    List<Long> getAllUserColectionByStatusAndDay(@Param("userIds") List<Long> userIds, @Param("status") int status, @Param("day") Integer day);

    /**
     * 获取指定状态的所有用户id  已用户分组(上周)
     *
     * @param userIds
     * @param status
     * @param beginOfWeek
     * @param endOfWeek
     * @return
     */
    List<Long> getAllUserColectionByStatusAndLastWeek(@Param("userIds") List<Long> userIds, @Param("status") int status, @Param("beginOfWeek") Date beginOfWeek, @Param("endOfWeek") Date endOfWeek);

    /**
     * 获取day天内 指定的用户id 并且熔炼指定藏品的用户数量
     *
     * @param userIds
     * @param collectionIds
     * @param status
     * @param days
     * @return
     */
    List<Long> getHoldCollectionByStatusAndDay(@Param("userIds") List<Long> userIds, @Param("collectionIds") List<Long> collectionIds, @Param("status") int status, @Param("days") int days);

    /**
     * 获取上周天内 指定的用户id 并且熔炼指定藏品的用户数量
     *
     * @param userIds
     * @param collectionIds
     * @param status
     * @return
     */
    List<Long> getHoldCollectionByStatusAndLastWeek(@Param("userIds") List<Long> userIds, @Param("collectionIds") List<Long> collectionIds, @Param("status") int status, @Param("beginOfWeek") Date beginOfWeek, @Param("endOfWeek") Date endOfWeek);

    @Select("select count(distinct user_id) from sc_user_ecology_hatch_records where is_delete=0 ")
    Integer getSumEcologyUserCount();

    @Select("select count(distinct user_id) from sc_user_ecology_hatch_records where is_delete=0 and create_time between #{startTime} and #{endTime} ")
    Integer getSumEcologyUserCountByDate(@Param("startTime") Date startTime, @Param("endTime") Date endTime);

    @Select("select collection_name as name , count(distinct user_id) as count ,sum(use_count)  as usecount from sc_user_ecology_hatch_records where is_delete=0 group by collection_name")
    List<Map<String, String>> getSumCollectionEcologyCount();

    /**
     * 查询总孵化积分
     *
     * @return
     */
    @Select("select IFNULL(sum(ecology_points),0) from sc_user_ecology_hatch_records where is_delete=0  AND create_time < CURDATE( )")
    BigDecimal getSumEcologyPoints();

    /**
     * 查询昨日孵化总积分
     *
     * @return
     */
    @Select("select IFNULL(sum(ecology_points),0) from sc_user_ecology_hatch_records WHERE DATEDIFF(create_time,NOW())=-1")
    BigDecimal getYesterDaySumEcologyPoints();

    /**
     * 根据时间查询投入燃料
     *
     * @param startTime
     * @param endTime
     * @return
     */
    BigDecimal getPutIntoFuelByStartTimeAndEndTime(@Param("startTime") Date startTime, @Param("endTime") Date endTime);

    /**
     * 分页查询用户记录
     *
     * @param page
     * @param collectionName
     * @param userTel
     * @param startTime
     * @param endTime
     * @return
     */
    Page<AdminUserEcologyHatchRcordsVO> userRecords(Page page, @Param("daoUserId") Long daoUserId, @Param("collectionId") Long collectionId, @Param("userTel") String userTel, @Param("startTime") String startTime, @Param("endTime") String endTime, @Param("methodName") String methodName);

    Page<AdminMetaUserEcologyHatchRcordsVO> adminUserRecords(Page page, @Param("collectionName") String collectionName, @Param("userTel") String userTel, @Param("startTime") String startTime, @Param("endTime") String endTim, @Param("methodName") String methodName);

    /**
     * 查询用户记录详情(汇总)
     *
     * @param id
     * @return
     */
    List<AdminUserEcologyHatchRcordsDetailsVO> userRecordsDetails(@Param("id") Long id);

    /**
     * 查询所有用户投入的燃料
     *
     * @return
     */
    List<SumEcologyPointsGroupUserDTO> getSumEcologyPointsGroupUserId();

    /**
     * 根据藏品id查询投入燃料
     *
     * @param collectionId
     * @return
     */
    Integer getSumUserCount(@Param("collectionId") Long collectionId);

    /**
     * 根据藏品id查询投入燃料
     *
     * @param collectionId
     * @return
     */
    BigDecimal getSumEcologyPointsByCollectionId(@Param("collectionId") Long collectionId);

    /**
     * 分页获取熔炼记录
     *
     * @param page
     * @param userTel
     * @param startTime
     * @param endTime
     * @return
     */
    Page<AdminShareProfitCollectDetailsVO> getPageByUserTelAndTime(Page page, @Param("collectionId") Long collectionId, @Param("userTel") String userTel, @Param("startTime") Date startTime, @Param("endTime") Date endTime);


    /**
     * 获取某个用户下 所有熔炼金额的总和
     *
     * @param userId
     * @param collectionId
     * @param toDay
     * @return
     */
    BigDecimal getSumPriceByUserIdAndCollectionIdAndTime(@Param("daoUserId") Long daoUserId, @Param("collectionId") Long collectionId, @Param("toDay") DateTime toDay, @Param("serviceCenterUserId") Long serviceCenterUserId, @Param("daoOnlineTime") Date daoOnlineTime);

    /**
     * 获取指定天数的平台业绩 不包含当天
     *
     * @param day
     * @return
     */
    BigDecimal getPlatformEffectivePerformanceByDay(@Param("day") Integer day);

    /**
     * 获取上周平台业绩
     *
     * @param weekBegin
     * @param weekEnd
     * @return
     */
    BigDecimal getPlatformEffectivePerformanceByLastWeek(@Param("weekBegin") DateTime weekBegin, @Param("weekEnd") DateTime weekEnd);

    /**
     * 查询超过半小时 还没有分成的记录
     *
     * @param isSettleDivideInto
     * @return
     */
    List<UserEcologyHatchRecords> getListByisSettleDivideIntoAndTime(@Param("isSettleDivideInto") Boolean isSettleDivideInto);

    /**
     * 用户熔炼记录导出
     *
     * @param daoId
     * @param collectionName
     * @param userTel
     * @param startTime
     * @param endTime
     * @return
     */
    List<AdminUserEcologyHatchRcordsVO> userRecordsExport(Long daoId, String collectionName, String userTel, String startTime, String endTime);

    /**
     * 获取下级熔炼记录的人数
     *
     * @param recommendUserId
     * @param status
     * @param id
     * @return
     */
    List<Integer> getCountByStatusAndJuniorUserIdsAndId(@Param("recommendUserId") Long recommendUserId, @Param("status") Integer status, @Param("id") Long id);

    /**
     * 查询用户熔炼某款仓库的总数
     *
     * @param userId
     * @param status
     * @param ecologyHatchId
     * @return
     */
    Integer getCountByStatusAndUserIdAndId(@Param("userId") Long userId, @Param("status") int status, @Param("ecologyHatchId") Long ecologyHatchId);



    Integer getSumHatchCountByUserId (@Param("userId") Long userId);

    /**
     * 获取用户熔炼总额(取发行价)
     *
     * @param usersWhoExcludeUsersFromCommunityStudiosAndCityServiceCenters
     * @return
     */
    BigDecimal getUserIdsSmeltMarketValue(@Param("userIds") List<Long> usersWhoExcludeUsersFromCommunityStudiosAndCityServiceCenters);

    List<UserHatchCountDTO> getListByGroupUserIdAndCollectionId(@Param("collectionId") Long collectionId);

    /**
     * 获取伞下团对熔炼数据
     *
     * @param daoUserId
     * @param collectionId
     * @param userTel
     * @return
     */
    AdminTeamHatchDataVO getPageTeamHatchData(@Param("daoUserId") Long daoUserId, @Param("collectionId") Long collectionId, @Param("userId") Long userId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    /**
     * 查询某种藏品熔炼次数
     *
     * @param collectionId
     * @return
     */
    Integer getSumHatchCountByCollectionId(@Param("collectionId") Long collectionId);

    /**
     * 查询熔炼用户数
     *
     * @param collectionId
     * @return
     */
    Integer getHatchUserCountByCollectionId(@Param("collectionId") Long collectionId);

    BigDecimal getSumMarketValueLessThan(@Param("min") Integer min, @Param("max") Integer max);

    BigDecimal getSumUserCountLessThan(@Param("min") Integer min, @Param("max") Integer max);

    /**
     * 在国库活动中-活动期间内所产出的所有的熔炼值
     *
     * @param userId
     * @param startTime
     * @param endTime
     * @return
     */
    BigDecimal getSmeltingValue(@Param("userId") Long userId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    /**
     * 获取已熔炼数量
     *
     * @param ecologyHatchId
     * @return
     */
    Integer getHatchCountByEcologyHatchId(@Param("ecologyHatchId") Long ecologyHatchId);


    /**
     * 获取市值管理所需平台用户熔炼的份数
     *
     * @param ecologyHatchId
     * @return
     */
    Integer userInPlatformNum(@Param("ecologyHatchId") Long ecologyHatchId, @Param("platformDaoId") Long platformDaoId);


    /**
     * 获取市值管理品牌熔炼的份数
     *
     * @param ecologyHatchId
     * @return
     */
    Integer userInDaoNum(@Param("ecologyHatchId") Long ecologyHatchId, @Param("daoIds") List<Long> daoIds);

    /**
     * 国库活动社区网点所产出的熔炼值
     *
     * @param userId
     * @param startTime
     * @param endTime
     * @return
     */
    BigDecimal getUserWorkRoomExcludeExchequerList(@Param("userId") Long userId, @Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("collectionIdList") List<Long> collectionIdList);

    /**
     * 国库活动城市服务中心所产出的熔炼值
     *
     * @param userId
     * @param startTime
     * @param endTime
     * @return
     */
    BigDecimal getUserCityServerExcludeExchequerList(Long userId, Date startTime, Date endTime, List<Long> collectionIdList);


    List<UserEcologyHatchRecords> getScriptNeedList();

    List<UserEcologyHatchRecords> getScriptNeedAllList();

    List<UserEcologyHatchRecords> getAllByEcologyHatchIdList(@Param("ecologyHatchId") Long ecologyHatchId);



    List<HatchCountPageVO> getHatchCountVO(@Param("userCreatorId") Long userCreatorId);

    /**
     * 获取用户熔炼总组数
     * @param userId
     * @return
     */
    Integer getUserSum(@Param("userId") Long userId, @Param("collectionId") Long collectionId);

    /**
     * 获取用户限制时间内熔炼总组数
     * @param userId
     * @return
     */
    Integer getLimitsTheUserSum(@Param("userId") Long userId);


    List<UserEcologyHatchRecords> otherScriptList();

    List<UserEcologyHatchRecords> scriptList();

    void updateScriptList();
    @Select("select ifnull(sum(hatch_count),0) from sc_user_ecology_hatch_records where user_id=#{userId} and is_delete=0 ")
    Integer getAllUserSum(@Param("userId") Long userId);


    List<EcologySmeltVO> ecologySmeltCount(@Param("daoId") Long daoId,@Param("startTime") Date startTime,@Param("endTime") Date endTime);

    Long getUnderSumSmeltCount(@Param("userId") Long userId,@Param("collectionId") Long collectionId,@Param("status") Integer status);

    BigDecimal getDynamicRewardAmount(@Param("pointsId") Long pointsId, @Param("startTime") Date startTime,@Param("endTime") Date endTime);


    List<Long> getListByUserIdsAndCollectionIdAndStatusAndGtCount(@Param("userIds") List<Long> userIds,
                                                                  @Param("status") Integer status,
                                                                  @Param("count") int count,
                                                                  @Param("hatchCollectionId") Long hatchCollectionId);

    List<UserEcologyDTO> getBooleanByStatusAndUserIdListPoints(@Param("userIds") List<Long> userIds, @Param("code") int code, @Param("hatchCollectionId") Long hatchCollectionId);

    int getCountByUserIdsAndStatusAndDayAndCollectionId(@Param("userIds") List<Long> userIds, @Param("status") int status, @Param("day") Integer day, @Param("hatchCollectionId") Long hatchCollectionId);

    int getCountByUserIdsAndStatusAndLastWeekAndCollectionId(@Param("userIds") List<Long> userIds, @Param("status") int status, @Param("beginOfWeek") DateTime beginOfWeek, @Param("endOfWeek") DateTime endOfWeek, @Param("hatchCollectionId") Long hatchCollectionId);

    List<Long> ecologyIngServiceCenter(@Param("userId") List<Long> allUserCityServerCentreUserId, @Param("code") int code, @Param("collectionId") Long collectionId);

    List<EcologyGetDestructionDTO> getCountByCollectionIdWithDestruction(@Param("collectionId") Long collectionId);
}
